import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;

public class ChatWorkerThread implements Runnable {
	private ChatListenThread chatListenThread;
	private Socket socket;

	public ChatWorkerThread(ChatListenThread chatListenThread, Socket socket) {
		this.chatListenThread = chatListenThread;
		this.socket = socket;
	}

	public void run() {
		try {
			DataInputStream din = new DataInputStream(socket.getInputStream());
			while (true) {
				String message = din.readUTF();
				System.out.println("Sending " + message);
				chatListenThread.sendToAll(message);
			}
		} catch (EOFException ie) {
		} catch (IOException ie) {
			ie.printStackTrace();
		} finally {
			chatListenThread.removeConnection(socket);
		}
	}
}